package com.dbb.tag;

import java.io.IOException;
import java.io.Writer;

import org.apache.struts2.components.Component;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.util.ValueStack;

public class PageComponent extends Component {
	private String urlNode = "<a href='javascript:jumpPage({})' {}>{}</a>";
	private Page page;
	private long pageNum = 0;
	
	public PageComponent(ValueStack stack) {
		super(stack);
	}

	public long getPageNum() {
		return pageNum;
	}

	public void setPageNum(long pageNum) {
		this.pageNum = pageNum;
	}



	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	//将自己需要输出的逻辑通过writer输出字符串就可以了。
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			writer.write(toHTML(page));
		} catch (IOException ex) {
			System.out.println("writer.write(toHTML(page));Error");
		}

		return result;
	}
	protected String toHTML(Page page) {
		if(page == null) {
			return "";
		}

		StringBuffer sb = new StringBuffer();
		sb.append("<div class='pager'>");
		sb.append("<a href='javascript:jumpPage(1)'>First</a>");
		if(page.isHasPre()){
			sb.append("<a href='javascript:jumpPage(" + page.getPrePage() + ")'>Pre</a>");
		}

		if(page.getTotalPages() < getPageNum()){
			for(long i = 0; i < page.getTotalPages(); i++){
				sb.append("<a href='javascript:jumpPage(" + (i + 1) + ")' " + ((page.getPageNo() == (i+1))?"class='cur'":"") + "'>" + (i + 1) + "</a>");
			}
		} else if(page.getPageNo() < getPageNum()/2){
			for(long i = 0; i < getPageNum(); i++){
				sb.append("<a href='javascript:jumpPage(" + (i + 1) + ")' " + ((page.getPageNo() == (i+1))?"class='cur'":"") + "'>" + (i + 1) + "</a>");
			}
		} else if(page.getTotalPages() - page.getPageNo() < getPageNum()/2 ) {
			for(long i = 0; i < getPageNum(); i++){
				sb.append("<a href='javascript:jumpPage(" + (page.getTotalPages() - getPageNum() + 1) + ")' " + ((page.getPageNo() == (i+1))?"class='cur'":"") + "'>" + (page.getTotalPages() - getPageNum() + 1 + i) + "</a>");
			}
		} else {
			for(long i = 0; i < getPageNum(); i++){
				sb.append("<a href='javascript:jumpPage(" + (page.getPageNo() - getPageNum()/2 + 1) + ")' " + ((page.getPageNo() == (i+1))?"class='cur'":"") + "'>" + (page.getPageNo() - getPageNum()/2 + 1) + "</a>");
			}
		}

		if(page.isHasNext()){
			sb.append("<a href='javascript:jumpPage(" + page.getNextPage() + ")'>Next</a>");
		}
		sb.append("<a href='javascript:jumpPage(" + page.getTotalPages() + ")'>End</a>");
		sb.append("</div>");
		return sb.toString();
	}
	
	/**
	 * 分页Html字符串组装
	 * 
	 * @param page
	 * @return
	 */
	private String paginationString(Page page) {
		StringBuffer pageStr = new StringBuffer("<div class=\"pagernumber\">");
		System.out.println("page:" + page.getTotalCount());
		
		if(page == null) {
			page = new Page();
		}
		o(page.getTotalCount());
		o(page.getPageNo());
		if (page.getTotalPages() > 1l) {
			if (page.getPageNo() > 1) {
				pageStr.append("<a href=\"javascript: jumpPage('1');\">首页</a>");
				pageStr.append("<a href=\"javascript: jumpPage('" + (page.getPageNo() - 1) + "');\">上一页</a>");
			}
			if (page.getTotalPages() <= 10l) {// 总页数小于10的
				for (int i = 1; i <= (int)page.getTotalPages(); i++)
					concat(pageStr, i, page);
			} else {
				int index = page.getPageNo();
				if (page.getPageNo() <= 5) {// 小于5的前面的链接都有
					for (int i = 1; i <= 7; i++)
						concat(pageStr, i, page);
					pageStr.append("...");
				} else if (page.getPageNo() > 5 && page.getPageNo() <= page.getTotalPages() - 4) {
					concat(pageStr, 1, page);// 第一页
					pageStr.append("...");
					for (int i = -2; i <= 2; i++)
						concat(pageStr, index + i, page);
					pageStr.append("...");
				} else if (page.getPageNo() >= page.getTotalPages() - 4) {
					concat(pageStr, 1, page);// 第一页
					pageStr.append("...");
					for (int i = -(page.getPageNo() - ((int)page.getTotalPages() - 5)); (index + i) <= (page.getTotalPages() - 1); i++)
						concat(pageStr, index + i, page);
				}
				concat(pageStr, (int)page.getTotalPages(), page);
			}
			if (page.getPageNo() < page.getTotalPages()) {
				pageStr.append("<a href=\"javascript: jumpPage('" + (page.getPageNo() + 1) + "');\">下一页</a>");
				pageStr.append("<a href=\"javascript: jumpPage('" + page.getTotalPages() + "');\">尾页</a>");
			}
		}
//		pagination.append("<p><span>共<font color='red'>" + pageInfo.getRowCount() + "</font>条</span>&nbsp;&nbsp;&nbsp;<span>共<font color='red'>" + pageInfo.getPageCount() + "</font>页</span></p>");
		pageStr.append("</div>");
		return pageStr.toString();
	}
	
	void o(Object o) {
		System.out.println(o);
	}

	private void concat(StringBuffer pagination, int pageNo, Page page) {
		if (pageNo == page.getPageNo()) {
			String classStyle = "class=\"cur\"";
			pagination.append("<a href=\"javascript: jumpPage('" + pageNo + "');\" " + classStyle + ">" + pageNo + "</a>");
			return;
		}
		pagination.append("<a href=\"javascript: jumpPage('" + pageNo + "');\">" + pageNo + "</a>");
	}

}
